<%@ page isELIgnored="false" %><%--isELIgnored=true禁用EL表达式--%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!-- JSTL标签库是一个JSP标签的集合，封装了许多jsp应用程序通用的核心功能-->
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/layui-v2.6.13/layui/css/layui.css"/>
    <script src="${pageContext.request.contextPath}/js/jquery.min-2.1.1.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/layui-v2.6.13/layui/layui.js"></script>
    <style>
        .layui-form{
            margin-top: 30px;
            margin-left: 30px;
        }
        #d1{
            margin-left: 30px;
        }
    </style>
</head>
<body>
<form class="layui-form" action="">
    <input type="hidden" name="id" class="layui-input" value="${goods.id}">
    <div class="layui-form-item" style="text-align: center;">
        <label class="layui-form-label">商品名称</label>
        <div class="layui-input-inline">
            <input type="text" name="gName" class="layui-input" value="${goods.gName}">
        </div>
        <label class="layui-form-label">上传图片</label>
        <div class="layui-input-inline">
            <button type="button" class="layui-btn" id="imgUpload">

                <input type="hidden" id="imgUrl" name="imgUrl" class="layui-input" value="">
                <i class="layui-icon">&#xe67c;</i>上传图片
            </button>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">库存</label>
        <div class="layui-input-inline">
            <input type="text" name="gCount" class="layui-input" value="${goods.gCount}">
        </div>
        <label class="layui-form-label">单价</label>
        <div class="layui-input-inline">
            <input type="text" name="gPrice" class="layui-input" value="${goods.gPrice}">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">一级分类</label>
        <div class="layui-input-inline">
            <select name="" lay-filter="f1">
<%--                <option value="">一级分类</option>--%>
                <c:forEach items="${first}" var="first">
                    <option
                            <c:if test="${goods.parentId == first.id}"> selected=true </c:if>
                            value="${first.id}">${first.sName}</option>
                </c:forEach>
            </select>
        </div>
        <label class="layui-form-label">二级分类</label>
        <div class="layui-input-inline">
            <select name="sId" id="second">
<%--                <option value=''>二级分类</option>--%>
                <c:forEach items="${second}" var="second">
<%--                    <c:if test="${goods.parentId == second.parentId}">--%>
                        <option
                                <c:if test="${goods.sId == second.id}"> selected=true </c:if>
                                value="${second.id}">${second.sName}</option>
<%--                    </c:if>--%>
                </c:forEach>
            </select>
        </div>

    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">货物状态</label>
        <div class="layui-input-inline">
            <input type="radio" name="gStatus" value="1" <c:if test="${goods.gStatus == 1}">checked=true</c:if> title="上架">
            <input type="radio" name="gStatus" value="0" <c:if test="${goods.gStatus == 0}">checked=true</c:if> title="下架" >
        </div>

    </div>
</form>
<div class="layui-form-item" id="d1">
    <div class="layui-input-block">
        <button class="layui-btn" style="width: 500px;"onclick="update()">更新</button>
    </div>
</div>
</body>
<script>
    layui.use('upload', function(){
        var upload = layui.upload;
        //执行实例
        upload.render({
            elem: '#imgUpload' //绑定元素
            ,url: 'upload' //上传接口
            ,accept: 'images' //指定允许上传时校验的文件类型，可选值有：images（图片）、file（所有文件）、video（视频）、audio（音频）
            ,acceptMime: 'image/*' //规定打开文件选择框时，筛选出的文件类型，值为用逗号隔开的 MIME 类型列表
            ,done: function(res){//上传完毕回调,res必须是json格式
                console.info(res);
                console.info(res.imgUrl);
                var url = res.imgUrl;
                var imgUrl = new Array();
                imgUrl = url.split('/');
                console.info(imgUrl)

                // $("#i1").attr("value","1");
                $("#imgUrl").attr("value","http://127.0.0.1:8887/"+imgUrl[2])  ;
                console.info($("#imgUrl").val())
                if (res.code == 1){
                    alert("上传成功")
                }else {
                    alert("上传失败")
                }
            }
            ,error: function(){
                //请求异常回调
            }
        });
    });
    function update(){
        var params = $("form").serialize();
        console.info(params);
        $.ajax({
            url: "http://localhost:8080/shopping/goods/updateOneGoods",
            data: params,
            type: "post",
            success: function(code){
                if(code == '1'){
                    alert("修改成功");

                    window.parent.location.reload();
                } else {
                    alert("修改失败");
                }
                //延迟关闭
                // setTimeout(closeLayer(), 500);
            }
        });
    }
    layui.use(['layer','form'], function(){
        var layer = layui.layer;
        var form = layui.form;
        form.on('select(f1)', function (data) {
            console.info(data.elem); //得到select原始DOM对象
            console.info(data.value); //得到被选中的值

            var params = "parentId=" + data.value;
            $.ajax({
                url: "http://localhost:8080/shopping/goods/querySecond",
                data: params,
                type: "post",
                success: function (obj) {
                    //将json字符串转为数组
                    var arr = JSON.parse(obj);

                    //清空子元素
                    $("#second").empty();
                    $("#second").append("<option value=''>二级分类</option>");
                    //添加子元素
                    $.each(arr, function (index, item) {
                        console.info(item);
                        //在元素内部末尾插入子元素
                        $("#second").append(
                            "<option value='" + item.id + "'>" + item.name
                            + "</option>");
                    });
                    //重新渲染select下拉框
                    form.render('select');
                }
            });

        });
    });
</script>
</html>